$(function () {
    //获取url
    var url=localStorage.getItem("ajaxUrl");
    var cpmpanyCode=localStorage.getItem("cpmpanyCode");
    var pass=1;


    //根据缓存的角色内容F
    var arr_role=JSON.parse(localStorage.getItem("arr_role")) ? JSON.parse(localStorage.getItem("arr_role")) : {};

    $(".con ul li").hide();
    $(".con ul li.switchCompany").show();
    for (var k in arr_role) {
        if (k && $(".con ul li." + k)) {
            $(".con ul li." + k).show();
        }
    }

    var companys = JSON.parse(localStorage.getItem("companys")) ? JSON.parse(localStorage.getItem("companys")) : {};
    var companyHtml = "";
    for (var i in companys) {
        companyHtml += '<option value="'+ i +'">'+ companys[i].companyNameShort +'</option>'
    }
    $("div.div_gs select.companys").html(companyHtml).val(cpmpanyCode).trigger("change");

    //点击 切换公司
    $("li.switchCompany").on("click", function () {
        $(".mengceng").show();
        $("div.div_gs").show();
        $("div.div_gs select.companys").val(cpmpanyCode).trigger("change");
    });
    //点击 切换公司 取消
    $("div.div_gs .gs_quxiao").on("click", function () {
        $(".mengceng").hide();
        $("div.div_gs").hide();
    });
    //点击 切换公司 确认
    $("div.div_gs .gs_queding").on("click", function () {
        var company = $("div.div_gs select.companys").val();
        if (!company) {
            return $alert("请选择切换的公司");
        } else {
            loading_show("切换中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/user/switchCompany',
                data: {
                    companyCode: company
                },
                dataType: 'json',
                timeout: 60000,
                json: 'callback',
                success: function (res) {
                    if (res.success) {
                        //加载提示符 隐藏
                        loading_hide();
                        //console.log(data);
                        $alert(res.companys[res.company].companyNameShort + "，切换成功", 1000);
                        localStorage.setItem("companys", JSON.stringify(res.companys));
                        localStorage.setItem("cpmpanyCode", res.company);
                        localStorage.setItem("jwt", res.jwt);
                        setTimeout(function () {
                            window.location.href = "role.html";
                        }, 1000);
                    } else {
                        //加载提示符 隐藏
                        loading_hide();
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    //加载提示符 隐藏
                    loading_hide();
                    $alert(error.message);
                }
            });
        }
    });

});
